<template>
  <div class="residentManagement">
    <el-row>
      <el-col style="display:flex;align-items: center;margin-bottom:10px;"><div class="border"></div><h3>{{residentManagement.title}}</h3></el-col>
    </el-row>
    <el-row> 
      <el-col :span="16">
      <div style="display:flex;align-items: center;margin-left:20px">
          <el-button  class="addBtn boxShadowButton" icon="el-icon-circle-plus" style="margin-right:40px" @click="add">{{residentManagement.add}}</el-button>
          <p style="width:40px;">{{residentManagement.search}}</p>
          <el-input style="width:260px;margin-right:5px" :placeholder="residentManagement.oldName" v-model.trim="oldName" @keyup.enter.native.prevent="search"></el-input>
           <el-button class="searchBtn boxShadowButton" @click="search">{{residentManagement.check}}</el-button>
           <el-button class="searchBtn boxShadowButton" @click="print">{{residentManagement.print}}</el-button>
      </div>
      </el-col>
        <el-col align="right" :span="8">
         <el-button class="prevPage boxShadowButton"  :disabled="pageNum==firstPage" @click="prevePage">{{residentManagement.prevePage}}</el-button>
           <el-button class="nextPage boxShadowButton"  :disabled="pageNum==lastPage"  @click="nextPage">{{residentManagement.nextPage}}</el-button>
      </el-col>
    </el-row>
     <el-table ref="tableList" :data="tableData"  style="width: 100%;margin-top:10px;margin-left:20px"  :row-style="tableRowStyle" :header-cell-style="tableHeaderColor">
        <el-table-column prop="oldmanNo" :label="residentManagement.sort" min-width="120" align="center"></el-table-column>
        <el-table-column prop="roomNo"  :label="residentManagement.roomNo" min-width="120" align="center"></el-table-column>
        <!-- 测试说要注释掉 -->
        <!-- <el-table-column prop="floor"  :label="residentManagement.manNumber" min-width="120" align="center"></el-table-column> -->
        <el-table-column prop="buildingName"  :label="residentManagement.build" min-width="120" align="center"></el-table-column>
        <el-table-column prop="checkStat" :formatter="formatStat" :label="residentManagement.status" min-width="120" align="center"></el-table-column>
        <el-table-column prop="oldName" :label="residentManagement.name" min-width="120" align="center"></el-table-column>
        <el-table-column  :label="residentManagement.dosth" min-width="220" align="center">
          <template slot-scope="scope">
            <el-button class="detailBtn boxShadowButton" @click="detailClick(scope.row)"  :size="size">{{residentManagement.detail}}</el-button>
            <el-button class="deleteBtn boxShadowButton" @click="delet(scope.row)" :size="size">{{residentManagement.delet}}</el-button>
          </template>
        </el-table-column>
      </el-table>

     <!-- 删除 -->
    <el-dialog
      :title="residentManagement.delete"
      :visible.sync="deletDialogVisible"
      width="30%"
      :center="true"
      >
      <h3 style="text-align: center">{{residentManagement.isDelet}}</h3>
      <span slot="footer" class="dialog-footer">
        <el-button
            class="deleteBtn boxShadowButton"
            @click="checkDelet"
            :size="size"
          >{{residentManagement.yes}}</el-button>
        <el-button class="jiebangBtn boxShadowButton" :size="size"  @click="deletDialogVisible = false">{{residentManagement.no}}</el-button>
        
      </span>
    </el-dialog>

    <!-- 分页 -->
    <el-pagination
      background
      layout="total,prev, pager, next,jumper"
      :total="total"
      style="margin-top:20px"
      align= "center"
       :current-page='currentPage'
      @current-change="changePage">
    </el-pagination>
  </div>  
</template>
<script>
import bus from "../../../vuex/eventBus"; //引入eventBus,接收子界面传递的跳转指令,然后在父界面跳转,添加tab
export default {
  name:"ResidentManagement",
  data() {
    return {
      residentManagement: this.$t('m.residentManagement'),//引入 lang 里面的字符串资源,
      deletDialogVisible:false,
      id:0,
      size:"mini",
      oldName:'', //搜索框老人姓名
      tableData:[],
      total:0,
      pageNum:1,
      pageSize:10,
      currentPage:1,
       firstPage:'',//第一页
      lastPage:'',//最后一页
      roomId:"",
      DefaultSetting:{},
      acceptLanguage:'ja_JP',//数据请求传参需要传的语言  ja_JP----日文

    }
  },
  methods:{
    formatStat(row,index){
      if(row.checkStat == 1){
        return '入居中';
      }else{
        return '一時外出中';
      }
    },
     //跳转详情
    detailClick(row){
      sessionStorage.setItem("oldmanId",row.oldmanId);
      sessionStorage.setItem("intoType","fix");
       bus.$emit("homePage", {
            type: "jump",
            path: "/ResidentDetail",
            title:this.residentManagement.fix,
      });
    },

    //打印
    print(){
      this.$print(this.$refs.tableList);
    },

     //上一页
    prevePage(){
      this.pageNum= this.pageNum>this.firstPage ? this.pageNum-1 : this.firstPage;
      this.getList()
    },

    //下一页
    nextPage(){
      this.pageNum= this.pageNum<this.lastPage ? this.pageNum+1 : this.lastPage;
      this.getList()
    },

    //添加
    add(){
      sessionStorage.setItem("intoType","add");
      bus.$emit("homePage", {
            type: "jump",
            path: "/ResidentDetail",
            title:this.residentManagement.add,
      });
    },

    //删除
    delet(row){
      this.deletDialogVisible =true;
      this.id = row.oldmanId;
      this.roomId=row.roomId;
    },

    //确认删除
    checkDelet(){
      this.$postForm('omc/lifeOldman/deleteById/'+this.id,{},this.acceptLanguage).then(res =>{
        if(res.code ==200){
          this.deletDialogVisible = false;
          this.initial();
          this.getList() 
        }
      })
    },

     //初始化居室设置
     //1.先获取机构默认值
    initial() {
      if (this.roomId) {
        console.log(this.roomId);
        this.$get(
          "dmc/defaultLifeTransducerConfig",
          { groupId: sessionStorage.getItem("groupId") },
          this.acceptLanguage
        ).then(res => {
          console.log(res);
          this.DefaultSetting = res;
          this.initialSensor(this.roomId);
        });
      } else {
        this.$message({ message: this.residentManagement.selectRoom, type: "error" });
      }
    },
    //初始化房间的sensor设置
    //2.将默认值初始化到房间
    initialSensor(roomId) {
      let params = {};
      params.roomId = roomId;
      params.toiletAttentionTime = String(
        this.DefaultSetting.toiletAttentionTime
      );
      params.toiletWarningTime = String(this.DefaultSetting.toiletWarningTime);
      params.toiletUnusualTime = String(this.DefaultSetting.toiletUnusualTime);
      params.temperatureMaximum = String(
        this.DefaultSetting.temperatureMaximum
      );
      params.temperatureMinimum = String(
        this.DefaultSetting.temperatureMinimum
      );
      params.breathMaxTimes = String(this.DefaultSetting.breathMaxTimes);
      params.breathMinTimes = String(this.DefaultSetting.breathMinTimes);
      params.heartbeatMinimum = String(this.DefaultSetting.heartbeatMinimum);
      params.heartbeatMaximum = String(this.DefaultSetting.heartbeatMaximum);
      params.vitalMeasuringTimeInterval = String(
        this.DefaultSetting.vitalMeasuringTimeInterval
      );
      params.leaveBedMeasuringTimeInterval = String(
        this.DefaultSetting.leaveBedMeasuringTimeInterval
      );
      params.walkMeasuringStartTime = String(
        this.DefaultSetting.walkMeasuringStartTime
      );
      params.walkMeasuringEndTime = String(
        this.DefaultSetting.walkMeasuringEndTime
      );
      params.walkMeasuringTimeInterval = String(
        this.DefaultSetting.walkMeasuringTimeInterval
      );
      params.cameraCheckTime = String(this.DefaultSetting.cameraCheckTime);
      params.leaveRoomAlertStartTime = String(
        this.DefaultSetting.leaveRoomAlertStartTime
      );
      params.leaveRoomAlertEndTime = String(
        this.DefaultSetting.leaveRoomAlertEndTime
      );
      params.beforeDuration = String(this.DefaultSetting.beforeDuration);
      params.afterDuration = String(this.DefaultSetting.afterDuration);
      params.temperatureMeasuringTimeInterval = "100";
      params.tumbleMeasuringFrame = "1";
      console.log(params);
      this.$put(
        "dmc/lifeTransducerConfig",
        params,
        "FORM-DATA",
        this.acceptLanguage
      ).then(res => {
        console.log(res);
        if (res.code == 200) {
          this.$message({ message: res.message, type: "success" });
        } else {
          console.log("____________________");
          this.$message({ message: res.message, type: "error" });
        }
      });
    },

    //搜索查询
    search(){
      this.pageNum = 1
      this.getList()
    },

    //查询列表
    getList(){
      let param ={
        pageNum:this.pageNum,
        pageSize:this.pageSize,
        oldName:this.oldName
      }
      this.$postForm('omc/lifeOldman/queryLifeOldmanWithPage',param,this.acceptLanguage).then(res =>{
         res.result.list.forEach(item =>{
           item.status = '入居中'
         })
        this.tableData = res.result.list
        this.total = Number(res.result.total)
        this.currentPage = res.result.pageNum;
        this.firstPage= res.result.firstPage//第一页
        this.lastPage= res.result.lastPage//最后一页
      })
    },

    //翻页
    changePage(val){
      this.pageNum = val
      this.getList()
    },

    /* 
      修改table tr行的背景色
    */
    tableRowStyle({ row, rowIndex }) {
      if (rowIndex % 2 == 0) {
        return "background-color: #efefef";
      } else {
        return "background-color: #f9f9f9";
      }
    },

    /* 
      修改table header的背景色
    */
    tableHeaderColor({ row, column, rowIndex, columnIndex }) {
      if (rowIndex === 0) {
        return "background-color:#ffffff;color:#000000;font-weight: 700;";
      }
    },
  },
  created() {

  },
  mounted() {
    this.getList()
    sessionStorage.removeItem("sessionStorage");
    bus.$on('changeResident',() =>{
       this.getList()
    })
  },
   destroyed() {
    bus.$off("changeResident");
  }
}
</script>
<style lang="scss" scoped>
.residentManagement{
  .prevPage{
      background:#409efe;
      color:#ffffff;
    }
    .nextPage{
      background: #ffffff;
    }
  .border{
    width: 10px;
    height: 30px;
    background: black;
    margin-right: 10px;
  }
  .addBtn{
    color:#7e7e7e;
  }
  .searchBtn{
    background: #409EFF;
    color: #ffffff
  }
  .detailBtn{
    background: #409EFF;
    color:#ffffff;
    border-radius: 5px;
  }
  .deleteBtn{
    background: #dddddd;
    color:#010101;
    border-radius: 5px;
  }
}
</style>